package com.maywide.jdp.system.servlet;

import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.jboss.cache.Cache;
import org.jboss.cache.Fqn;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.maywide.jdp.system.bean.BusinessFunctionBean;
import com.maywide.jdp.system.bean.OrganizationBean;
import com.maywide.jdp.system.bean.ParameterBean;
import com.maywide.jdp.system.cache.RuleCacheListener;
import com.maywide.jdp.system.entities.BusinessFunction;
import com.maywide.jdp.system.entities.Organization;
import com.maywide.jdp.system.entities.Parameter;
import com.maywide.jdp.system.entities.Role;
import com.maywide.jdp.system.entities.Rule;

/**
 * 初始化参数Servlet
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
public class SystemInitServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2482462085738955996L;
	private Cache<String, Object> cache;
	private OrganizationBean organizationBean = null;
	private BusinessFunctionBean businessFunctionBean = null;
	private ParameterBean parameterBean = null;

	private void fillOrganizationCache() throws Exception {
		cache.getRoot().addChild(Fqn.fromString(Organization.ROOT_CODE_TEXT));
		List<Organization> leafs = organizationBean.findAllLeaf();
		if (leafs != null && !leafs.isEmpty()) {
			for (Organization leaf : leafs) {
				String path = new StringBuffer(Organization.ROOT_CODE_TEXT)
						.append(leaf.getCodePath()).toString();
				cache.put(path, null);
			}
		}
	}

	private void fillFunctionCache() throws Exception {
		List<BusinessFunction> list = businessFunctionBean.findAllActive();
		if (list != null && !list.isEmpty()) {
			for (BusinessFunction func : list) {
				String key = new StringBuffer(func.getServerClass())
						.append(".").append(func.getServerFunction())
						.toString();
				List<Role> roles = func.getRoles();
				if (roles != null && !roles.isEmpty()) {
					for (Role role : roles) {
						String path = new StringBuffer("/roles/").append(key)
								.toString();
						cache.put(path, role.getId(), role);
					}
				}
				
				String beanPath = new StringBuffer("/rules/").append(func.getModule().getBeanName()).toString();
				List<Rule> rules = func.getRules();
				if (rules != null && !rules.isEmpty()) {
					String pointcutPath = new StringBuffer(beanPath).append("/").append(key).toString();
					for (Rule rule : rules) {
						cache.put(pointcutPath, rule.getId(), rule);
					}
				}
			}
		}
	}

	private void fillParametrCache() throws Exception {
		List<Parameter> list = parameterBean.findAllActive();
		if (list != null && !list.isEmpty()) {
			for (Parameter parameter : list) {
				Organization org = parameter.getEntity();
				String path = Organization.ROOT_CODE_TEXT;
				if (org != null)
					path = new StringBuffer(path).append(org.getCodePath())
							.toString();
				String key = parameter.getCode();
				String value = parameter.getData();
				cache.put(path, key, value);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void initBean(WebApplicationContext context) {
		this.cache = (Cache<String, Object>) context.getBean("cache");
		this.organizationBean = (OrganizationBean) context.getBean("organizationBean");
		this.businessFunctionBean = (BusinessFunctionBean) context.getBean("businessFunctionBean");
		this.parameterBean = (ParameterBean) context.getBean("parameterBean");
	}

	public void init(ServletConfig config) throws ServletException {
		WebApplicationContext context = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext());
		RuleCacheListener.setContext(context);
		initBean(context);
		//String root = config.getInitParameter("root");
		try {
			fillParametrCache();
			fillOrganizationCache();
			fillFunctionCache();
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
}
